package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Config;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.PatientDetail;
import com.qilu.pe.dao.bean.ProductPatientInfo;
import com.qilu.pe.dao.bean.Test;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.support.util.GlideUtil;
import com.qilu.pe.support.util.PeUtil;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 患者检测项目详情
public class PatientTestItemDetailActivity extends Base2Activity {
    protected RecyclerView rcv_test_item;
    private TextView tv_name, tv_sex, tv_age, tv_doctor, tv_num, tv_1, tv_state, tv_date, tv_price;
    private ImageView iv_sex, iv_header;

    private TestItemListAdapter adapter;
    private List<Test> testList = new ArrayList<>();

    private String id;
    private PatientDetail detail;
    private ProductPatientInfo mInfo;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_patient_test_item_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        rcv_test_item = findViewById(R.id.rcv_test_item);
        tv_name = findViewById(R.id.tv_name);
        tv_sex = findViewById(R.id.tv_sex);
        tv_age = findViewById(R.id.tv_age);
        tv_doctor = findViewById(R.id.tv_doctor);
        iv_sex = findViewById(R.id.iv_sex);
        iv_header = findViewById(R.id.iv_header);
        tv_num = findViewById(R.id.tv_num);
        tv_1 = findViewById(R.id.tv_1);
        tv_state = findViewById(R.id.tv_state);
        tv_date = findViewById(R.id.tv_date);
        tv_price = findViewById(R.id.tv_price);

        findViewById(R.id.tv_report_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("order_id", id);
                startActivity(ReportDetailActivity.class, bundle);
            }
        });

        setNavigationIcon(R.mipmap.ic_left_arrow);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            id = bundle.getString("id");
            detail = (PatientDetail) bundle.getSerializable("patientDetail");
        }

        if (null != detail) {
            tv_name.setText(detail.getName());
            tv_sex.setText(TextUtils.equals("1", detail.getSex()) ? "女" : "男");
            tv_age.setText("年龄:" + detail.getAge());
            tv_doctor.setText("负责人:" + detail.getU_name());
            iv_sex.setImageResource("1".equals(detail.getSex()) ?
                    R.mipmap.ic_patient_sex_female : R.mipmap.ic_patient_sex_male_2);// ic_patient_sex_female ic_patient_sex_male
            GlideUtil.loadImg(detail.getPicture(), iv_header);
        }

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new TestItemListAdapter(this);
        rcv_test_item.setLayoutManager(manager);
        rcv_test_item.setAdapter(adapter);

        setHeadTitle("患者项目详情");

        hideProgress();
        reqPatientProjectInfo();
        showProgress();
    }

    private Disposable reqPatientProjectInfo() {
        return APIRetrofit.getDefault().reqProductPatientInfo(Global.HEADER, id)
                .compose(RxUtil.<BaseResult2<ProductPatientInfo>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<ProductPatientInfo>>() {
                               @Override
                               public void accept(BaseResult2<ProductPatientInfo> res) throws Exception {
                                   hideProgress();
                                   if (res.isSuccess()) {
                                       mInfo = res.getData();
                                       tv_num.setText(String.format("共计%1$s项异常", mInfo.getAbnormal()));
                                       tv_1.setText(String.format("更新时间：%1$s", mInfo.getUpdated_at()));
                                       tv_state.setText(PeUtil.getOrderStatus(mInfo.getType()));
                                       tv_date.setText(mInfo.getTime());
                                       tv_price.setText(String.format("￥%1$s", mInfo.getPrice()));

                                       adapter.setDataList(mInfo.getPro_order());
                                   } else {
                                       ToastUtils.showShort(res.getMsg());
                                   }
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                hideProgress();
                                throwable.printStackTrace();
                            }
                        });
    }

    private class TestItemListAdapter extends ListBaseAdapter<ProductPatientInfo.ProOrder> {

        public TestItemListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_test_item_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView vName = holder.itemView.findViewById(R.id.tv_name);
            RoundedImageView vImg = holder.itemView.findViewById(R.id.iv_item_pic);
            TextView vItemName = holder.itemView.findViewById(R.id.tv_item_name);
            TextView vItem2 = holder.itemView.findViewById(R.id.tv_item_2);
            TextView vItem3 = holder.itemView.findViewById(R.id.tv_item_3);
            TextView vItem4 = holder.itemView.findViewById(R.id.tv_item_4);
            TextView vHint = holder.itemView.findViewById(R.id.tv_hint_info);

            ProductPatientInfo.ProOrder order = mDataList.get(position);

            vName.setText(order.getName());
            GlideUtil.loadImg(Config.URL_IMG + order.getPicture(), vImg);
            vItemName.setText("x" + order.getCount());
            vItem2.setText(order.getSample());
            vItem3.setText(order.getCapacity());
            vHint.setText(order.getSampling() == null ? "无" : order.getSampling());
        }
    }
}